// main.cpp
#include "file_operator.h"
#include <iostream>
#include <algorithm>

void print_result(const FileUtils::OperationResult& result) {
    std::cout << (result.success ? "SUCCESS" : "FAILED") << ": " << result.message << std::endl;
}

int main() {
    FileUtils utils;
    
    // 1. 创建测试目录和文件
    std::cout << "=== Creating test files and directories ===" << std::endl;
    print_result(utils.create_directory("test_dir"));
    print_result(utils.create_directory("test_dir/subdir"));
    print_result(utils.create_file("test_dir/test1.txt", "Hello World!"));
    print_result(utils.create_file("test_dir/test2.log", "Log content"));
    print_result(utils.create_file("test_dir/config.json", "{\"key\": \"value\"}"));
    print_result(utils.create_file("test_dir/subdir/nested.cpp", "#include <iostream>"));
    print_result(utils.create_file("test_dir/Image.PNG", "fake image content"));
    
    // 2. 基本搜索测试
    std::cout << "\n=== Basic search operations ===" << std::endl;
    
    // 使用正则表达式搜索
    // auto txt_files = utils.search_files("test_dir", ".*\\.txt$", false);
    auto txt_files = utils.search_files("test_dir", "test.*", false);
    std::cout << "Found .txt files (regex):" << std::endl;
    for (const auto& file : txt_files) {
        std::cout << "  " << file << std::endl;
    }
    
    
    // // 非递归搜索
    // auto non_recursive = utils.search_files_wildcard("test_dir", "*.*", false);
    // std::cout << "\nFiles in test_dir only (non-recursive):" << std::endl;
    // for (const auto& file : non_recursive) {
    //     std::cout << "  " << file << std::endl;
    // }
    
    // // 3. 高级搜索测试
    // std::cout << "\n=== Advanced search operations ===" << std::endl;
    
    // FileUtils::SearchOptions options;
    // options.pattern = "*.txt";
    // options.recursive = true;
    // options.use_regex = false;
    // options.case_sensitive = false;  // 大小写不敏感
    
    // auto advanced_results = utils.search_files_advanced("test_dir", options);
    // std::cout << "Advanced search (case-insensitive *.txt):" << std::endl;
    // for (const auto& file : advanced_results) {
    //     std::cout << "  " << file << std::endl;
    // }
    
    // // 按文件大小搜索
    // options.pattern = "*.*";
    // options.min_size = 5;  // 至少5字节
    // options.max_size = 100; // 最多100字节
    // options.case_sensitive = true;
    
    // auto size_results = utils.search_files_advanced("test_dir", options);
    // std::cout << "\nFiles between 5-100 bytes:" << std::endl;
    // for (const auto& file : size_results) {
    //     std::cout << "  " << file << " (size: " << utils.get_file_size(file) << " bytes)" << std::endl;
    // }
    
    // // 4. 其他操作测试
    std::cout << "\n=== Other operations ===" << std::endl;
    
    // 拷贝操作
    print_result(utils.copy("test_dir/test1.txt", "test_dir/test1_copy.txt"));
    
    // 重命名操作
    print_result(utils.rename("test_dir/test2.log", "renamed.log"));
    
    // 移动操作
    print_result(utils.create_directory("moved_files"));
    print_result(utils.move("test_dir/config.json", "moved_files/"));
    
    // 工具函数测试
    std::cout << "\n=== Utility functions ===" << std::endl;
    std::cout << "test_dir exists: " << utils.exists("test_dir") << std::endl;
    std::cout << "test_dir is directory: " << utils.is_directory("test_dir") << std::endl;
    
    if (utils.exists("test_dir/test1.txt")) {
        std::cout << "test1.txt absolute path: " << utils.get_absolute_path("test_dir/test1.txt") << std::endl;
        std::cout << "test1.txt size: " << utils.get_file_size("test_dir/test1.txt") << " bytes" << std::endl;
    }

    // 获取所有子目录
    std::cout << "\n--- Directories in test_structure ---" << std::endl;
    auto directories = utils.get_directories("test_dir",true);
    for (const auto& dir : directories) {
        std::cout << "DIR:  " << dir << std::endl;
    }
    
    // 获取所有文件
    std::cout << "\n--- Files in test_structure ---" << std::endl;
    auto files = utils.get_files("test_dir",true);
    for (const auto& file : files) {
        std::cout << "FILE: " << file << " (size: " << utils.get_file_size(file) << " bytes)" << std::endl;
    }
    
    // 获取所有内容（文件和目录）
    std::cout << "\n--- All contents in test_structure ---" << std::endl;
    auto contents = utils.get_directory_contents("test_dir",false);
    for (const auto& entry : contents) {
        std::string type = entry.is_directory ? "DIR " : "FILE";
        std::string size_info = entry.is_directory ? "" : " (" + std::to_string(entry.size) + " bytes)";
        std::cout << type << ": " << entry.path << size_info << std::endl;
    }


    
    // // 5. 清理
    // std::cout << "\n=== Cleanup ===" << std::endl;
    // print_result(utils.remove("test_dir"));
    // print_result(utils.remove("moved_files"));
    
    return 0;
}